require'zwsapi.i18n'
require'zwsapi.wrapper'
local html_forms = require("zwsapi.html_forms")
module(..., package.seeall)

DEFAULT_MARKUP_MODULE = "zwsapi.markup.markdown"

function new(app,nodeorg)
	local node = setmetatable({}, {__index=nodeorg})

	local markup_module_name
	if node.markup_module and node.markup_module:len() > 0 then
		markup_module_name = "zwsapi.markup."..node.markup_module
	else
		markup_module_name = app.config.MARKUP_MODULE or DEFAULT_MARKUP_MODULE
	end
	local markup_module = require(markup_module_name)
	node.markup = markup_module.new(app)
	
	-- Add methods to post error messages, notices, etc. Those become
	-- post_error(), post_warning(), etc.
	node.messages = {}
	for i, class in ipairs{"error", "warning", "success", "notice"} do
		node["post_"..class] = function(self, message) 
			table.insert(self.messages, {message=message, class=class})
		end
		node["post_translated_"..class] = function(self, key, details) 
			local message = self.translator.translate_key(key)
			if details then
				message = message.." ("..details..")"
			end
			table.insert(self.messages, {message=message, class=class})
		end
	end
	
	-- Create a table for storing headers and add a function to add headers to it.
	node.headers = {}
	node.add_header = function(self, header, value) self.headers[header] = value end
	
	-- Create a table for storing cookies and add a function to add cookies to it.
	-- The key of the cookie table should be the name of the cookie, while the
	-- value should be the value of the cookie, or the value false, in which
	-- case the cookie is deleted.
	
	node.cookies = {}
	node.set_cookie = function(self, name, value) self.cookies[name] = value end
	
	-- Add a function for redirecting. ::TODO:: check if this is used.
	node.redirect = function(node_self, url)
		node_self.headers["Location"] = url
	end
	
	-- Add tables and functions for CSS and Javascript.
	node.css_links = {}
	node.css_snippets = {}
	node.javascript_links = {}
	node.javascript_snippets = {}
	local function add(tab, key, values, defaults)
		if tab[key] then return end
		tab[key] = true
		table.insert(tab, values)
	end
	function node:add_css_link(href, media)
		media = media or "screen"
		return add(self.css_links, href.."|"..media, {href = href, media = media})
	end
	function node:add_css_snippet(href, snippet, media)
		media = media or "screen"
		return add(self.css_snippets, href.."|"..media, {snippet = snippet, media = media})
	end
	function node:add_javascript_link(href)
		add(self.javascript_links, href, {href=href})
	end
	function node:add_javascript_snippet(snippet)
		return add(self.javascript_snippets, snippet, {snippet=snippet})
	end
	
	function node:make_post_form(args, cfields, cfield_names)
		local post_timestamp = os.time()
		local post_token = app.auth:Timestamp_token(post_timestamp)
		local args = { 
					field_spec = args.field_spec,
					templates  = self.templates, 
					translator = self.translator,
					values	  = args.values,
					insert_hidden_fields = args.insert_hidden_fields,
					extra_fields = args.extra_fields,
					post_token = post_token,
					post_timestamp = post_timestamp,
					hash_fn	 = function(field_name)
										 return app:hash_field_name(field_name, post_token)
									 end
				}
		local html_for_fields, field_list = html_forms.make_html_form(args, cfields, cfield_names)
	
		return {
					post_timestamp = post_timestamp,
					post_token = post_token,
					html_for_fields = html_for_fields,
					field_list = field_list
		}
	end
	
	--------------------------------
	
	-- setup the page-specific translator
	for i, translation_node in ipairs(node.translations) do
		local translations = app.dal:GetNode(translation_node).content
		assert(type(translations) == "table",
				 "Could not load translation node")
		for k, translation in pairs(translations) do
			node.translations[k] = translation
		end
	end
	node.translator = zwsapi.i18n.make_translator(node.translations,
															 app.config.INTERFACE_LANGUAGE)

	-- translate the templates
	for i, template_node_id in ipairs(node.templates) do
		local template_node = app.dal:GetNode(template_node_id)
		local templates = template_node.content
		--templates.MAIN = template_node.main
		--templates.HEAD = template_node.head
		--templates.BODY = template_node.body
		assert(type(templates) == "table", "Could not load templates node")
		for k, template in pairs(templates) do
			node.templates[k] = node.translator.translate(template)
		end
	end
	node.wrappers=zwsapi.wrapper.wrappers
	return node
end

